﻿using System;
using System.Web.Mvc;
using JinianNet.JNCms.Data;
using System.Web.Routing;
using JinianNet.Common;
using UH = JinianNet.Common.Url;
using System.Collections.Generic;

namespace JinianNet.JNCms.Controllers
{
    public class HomeController : ControllerTemplatePage
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Page(string item)
        {
            if (Utils.IsNullOrEmpty(item) || !Utils.IsNumberOrLetter(item, true))
                return PageNotFound();
            this.RouteData.Values["path"] = UH.Concat("~", Config.Site.SiteDirectory, Config.Site.TemplateDirectory, Config.Site.Theme, string.Concat(item, Config.Site.TempLateSuffix));
            //ViewData["Message"] = string.Join("<br />", new string[]{
            //     System.Web.HttpRuntime.AppDomainAppId,
            //     System.Web.HttpRuntime.AppDomainAppPath,
            //     System.Web.HttpRuntime.AppDomainAppVirtualPath,
            //     System.Web.HttpRuntime.AppDomainId,
            //     System.Web.HttpRuntime.AspClientScriptPhysicalPath,
            //     System.Web.HttpRuntime.AspClientScriptVirtualPath,
            //     System.Web.HttpRuntime.AspInstallDirectory,
            //     System.Web.HttpRuntime.BinDirectory,
            //     System.Web.HttpRuntime.ClrInstallDirectory,
            //     System.Web.HttpRuntime.CodegenDir,
            //     System.Web.HttpRuntime.MachineConfigurationDirectory
            // });
            return View(item);
        }

        public ActionResult Article(string item)
        {
            IArticle dal = DataAccess.CreateArticle();
            JinianNet.JNCms.Models.Article model = dal.GetItem(item);
            if (model == null || !model.IsVerify || model.IsLock)
                return PageNotFound();
            if (model.Click < int.MaxValue)
            {
                dal.Click(model.ID);
            }
            if (model.IsUrl)
            {
                return Redirect(model.Url);
            }
            if (model.IsNeedPassword)
            {
                model.Content = "本文已经加密，请输入密码查看：<br /> <input type=\"text\" name=\"password\" /><input type=\"submit\" value=\"保存\" />";
            }
            ViewData["Article"] = model;
            Dictionary<string, object> site = ViewData["Site"] as Dictionary<string, object>;
            JinianNet.JNCms.Models.Category category = null;
            if (model.ChannelID > 0)
            {
                category = DataAccess.CreateCategory().GetItem(model.ChannelID);
            }
            if (category != null)
            {
                category = new JinianNet.JNCms.Models.Category();
            }
            site["Title"] = string.Join("-", model.Title, category.Title, site["Name"].ToString());// JNCms.Config.SeoRule.Article.Title.Replace("Article.Title", model.Title).Replace("Category.Title", category.Title).Replace("Site.Title", site.Name);
            site["KeyWords"] = model.KeyWords;// JNCms.Config.SeoRule.Article.KeyWords.Replace("Article.KeyWords", model.KeyWords).Replace("Category.KeyWords", category.KeyWords).Replace("Site.KeyWords", site.KeyWords);
            site["Description"] = model.Description;// JNCms.Config.SeoRule.Article.Description.Replace("Article.Description", model.Description).Replace("Category.Description", category.Description).Replace("Site.Description", site.Description);
            ViewData["Site"] = site;
            ViewData["Category"] = category;

            if (!Utils.IsNullOrEmpty(model.TempLate))
            {
                this.RouteData.Values["path"] = UH.Concat("~", Config.Site.SiteDirectory, Config.Site.TemplateDirectory, Config.Site.Theme, string.Concat(model.TempLate, Config.Site.TempLateSuffix));
            }
            return View();
        }


        public ActionResult Channel(string item, int page=1)
        {
            ICategory dal = DataAccess.CreateCategory();
            JinianNet.JNCms.Models.Category model = dal.GetItem(item);
            //if (model != null || !model.IsVerify)
            if (model == null || !model.IsVerify)
            {
                return PageNotFound();
            }
            if (model.IsUrl)
            {
                return Redirect(model.Url);
            }
            ViewData["Category"] = model;
            ViewData["List"] = DataAccess.CreateArticle().GetList(model.ID, page, Config.Page.Size);
            if (!Utils.IsNullOrEmpty(model.TempLate))
            {
                this.RouteData.Values["path"] = UH.Concat("~", Config.Site.SiteDirectory,  Config.Site.TemplateDirectory, Config.Site.Theme, string.Concat(model.TempLate, Config.Site.TempLateSuffix));
            }
            //string pageValue = page == 1 ? "" : page.ToString();
            Dictionary<string, object> site = ViewData["Site"] as Dictionary<string, object>;
            //site.Title = JNCms.Config.SeoRule.Article.Title.Replace("Article.Title", string.Empty).Replace("Category.Title", model.Title).Replace("Site.Title", site.Name).Replace("Page", pageValue);
            //site.KeyWords = JNCms.Config.SeoRule.Article.KeyWords.Replace("Article.KeyWords", string.Empty).Replace("Category.KeyWords", model.KeyWords).Replace("Site.KeyWords", site.KeyWords).Replace("Page", pageValue);
            //site.Description = JNCms.Config.SeoRule.Article.Description.Replace("Article.Description", string.Empty).Replace("Category.Description", model.Description).Replace("Site.Description", site.Description).Replace("Page", pageValue);
            //ViewData["Site"] = site;
            site["Title"] = string.Join("-", model.Title, site["Name"].ToString());// JNCms.Config.SeoRule.Article.Title.Replace("Article.Title", model.Title).Replace("Category.Title", category.Title).Replace("Site.Title", site.Name);
            site["KeyWords"] = model.KeyWords;// JNCms.Config.SeoRule.Article.KeyWords.Replace("Article.KeyWords", model.KeyWords).Replace("Category.KeyWords", category.KeyWords).Replace("Site.KeyWords", site.KeyWords);
            site["Description"] = model.Description;// JNCms.Config.SeoRule.Article.Description.Replace("Article.Description", model.Description).Replace("Category.Description", category.Description).Replace("Site.Description", site.Description);
            ViewData["Site"] = site;

            ViewData["WebPage"] = JinianNet.Web.PageLinker.GetPageLinker(string.Concat("/Channel/", model.KeyName),
                string.Concat("/Channel/", model.KeyName, "/{0}"),
                10,
                page,
                model.Count,
                Config.Page.Size,
                "首页",
                "上一页",
                "下一页",
                "末页");

            return View();
        }

        public ActionResult Attachment(int item,int page=0)
        {
            Models.Attachment model = DataAccess.CreateAttachment().GetItem(item);
            if(model==null)
                return PageNotFound();
            if (page == 1)
            {
                if (model.HasThumbnails)
                    return File(UH.Concat("~", Config.Site.SiteDirectory, "Resources/UploadFles", model.Folder, string.Concat(model.ThumbnailName, model.Extension)), model.Mime);
                else
                    return PageNotFound();
            }
            else
            {
                return File(UH.Concat("~", Config.Site.SiteDirectory, "Resources/UploadFles", model.Folder, string.Concat(model.SaveName, model.Extension)), model.Mime);
            }
        }
    }
}
